package cg.studio.algorithm.permutation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * there are two dimensions of implementation
 * 1. depends on different algorithm, there are insertion, list first characters and left characters
 * 2. coding way: iterative or Recursive
 * 
 * we should take duplicate characters into consideration.
 * 
 * @author Administrator
 *
 */
public class StringPermutation 
{
  public static void main( String[] argvs )
  {
    while( true )
    {
      try
      {
        System.out.println( "please input a String: " );
        BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
        String str = reader.readLine();
  
        System.out.println( "the permutation of '" + str + "' are: " );
        
        StringPermutation sp = new StringPermutation();
        sp.permutation( str );
      }
      catch( IOException e )
      {
        e.printStackTrace();
      }
    }
  }
  
  /**
   * implement permutation by insert characters into the word
   * @param word
   */
  public void permutation( String str )
  {
//    Permutator permutator = new InsertionPermutator();
//    Permutator permutator = new CalculatedPermutator();
    Permutator permutator = new RecursivePermutator();  //      //new ShiftPermutator();

    List< char[] > results = permutator.permutate( str );
    System.out.println( "the size of permutation is: " + results.size() );
    for( char[] result : results )
    {
      System.out.println( new String( result ) );
    }
  }
  
}
